apiready = function() {
seajs.use(['../../V',"../../api","../../jquery","../../app","../../service","../../pop"],function(V,$api,$,$app,service,$pop){
	var vm = {
		el: '.container',
	  	data: {
	    	list: ''
	  	},
	  	methods: {
	  		init: function(){
	  			this.getList()
	  		},
	  		getList: function(){
	  			var unionid = $api.getStorage("shop_unionid"), self = this;
		  		$app.ajax(service.addressGetList,{
		  			data: {
						values: {
							unionid: unionid
						}
					}
		  		},function(res){
		  			console.log(JSON.stringify(res));
		  			if(res.code == 0){
		  				if(res.data.length > 0){
		  					self.list = res.data;
		  					self.$refs.addressNull.style.display = "none";
		  				}else{
		  					self.list = '';
		  					self.$refs.addressNull.style.display = "block";
		  				}
		  			}else{
		  				alert(res.message || res.msg);
		  			}
		  		});
	  		},
	  		//--编辑
	  		addressListEdit: function(et){
	  			var id = et.currentTarget.dataset.id;
				$app.openWin('buyAddressEdit',{
  					address_id: id
  				});
	  		},
	  		//--删除
	  		addressListDel: function(et){
	  			var el = et.currentTarget;
				var id = +el.dataset.id;
				var index = +et.currentTarget.dataset.index;
				var unionid = $api.getStorage("shop_unionid");
				var self = this;
				$pop.msg({
					title: '地址删除',
					msg: '是否确定删除地址？',
					btn: ['取消','删除'],
					btnStyle: ['','background: #A46EFA;color: #fff;']
				},function(ret){
					if(ret.index == 1){
						$app.ajax(service.addressDel,{
				  			data: {
								values: {
									unionid: unionid,
									address_id: id
								}
							}
				  		},function(res){
				  			if(res.code == 0){
				  				self.list.splice(index, 1);;
				  				$pop.tip("删除成功");
				  				self.$refs.addressNull.style.display = self.list.length <=0 ? "block" : "none";
				  			}
				  		});
					}
				});
	  		},
	  		//--设为默认地址 addressDefault
	  		addressSetDef: function(et){
	  			var id = +et.currentTarget.dataset.id, self = this;
	  			var unionid = $api.getStorage("shop_unionid");
	  			$app.ajax(service.addressDefault,{
		  			data: {
						values: {
							unionid: unionid,
							address_id: id
						}
					}
		  		},function(res){
		  			if(res.code == 0){
		  				$pop.tip("设置成功",function(){
		  					self.init();
		  				});
		  			}
		  		});
	  		},
	  		//--新增
	  		addressNewAdd: function(et){
  				$app.openWin('buyAddressAdd');
	  		}
	  	},
	  	mounted: function() {
	  		var self = this;
	  		api.addEventListener({
			    name: 'addressAdd_'
			}, function(ret, err) {
			    self.init()
			});

			function btnKeyBack(){
				api.sendEvent({
					name:'addressList_'
				});
				$api.setTimeout(function(){
					api.closeWin();
				},300);
			}

			//--点击上面按钮返回
			$api.addEvt($api.dom('#btn-back-new'), 'click', function() {
	            btnKeyBack()
	        });
			//--安卓按键返回
			api.addEventListener({
				name: "keyback"
			},function(){
				btnKeyBack()
			});
			//--ios右滑返回
	        api.addEventListener({
			    name:'swiperight'
			},function(ret,err){
			    btnKeyBack();
         	});
	  	},
	  	created: function(){
	  		this.init();
	  	}
	};
	var even = function() {}
	var newVue = new Vue(vm);
	even();
});
}
